<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_Layer_constructor (t) {
        t.plan( 15 );
        
        var options = { chicken: 151, foo: "bar", projection: "none" };
        var layer = new OpenLayers.Layer('Test Layer', options);
        
        t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( layer.CLASS_NAME, "OpenLayers.Layer", "CLASS_NAME variable set correctly");

        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.id != null, "Layer is given an id");
        t.ok( layer.projection, "none", "default layer projection correctly set");
        t.ok( ((layer.chicken == 151) && (layer.foo == "bar")), "layer.options correctly set to Layer Object" );
        t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly backed up" );

        t.ok( typeof layer.div == "object" , "layer.div is created" );
        t.eq( layer.div.id, layer.id, "layer.div.id is correct" );

        options.chicken = 552;
        
        t.eq( layer.options["chicken"], 151 , "layer.options correctly made fresh copy" );
        
        t.eq( layer.isBaseLayer, false, "Default layer is not base layer" );

        layer = new OpenLayers.Layer('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        t.ok( layer.projection == null, "default layer projection correctly set");
        t.ok( layer.options instanceof Object, "layer.options correctly initialized as a non-null Object" );
    }


    function test_Layer_clone (t) {
        t.plan( 7 );
        
        var mapone = new OpenLayers.Map('map'); 
        var options = { chicken: 151, foo: "bar", maxResolution: "auto", visibility: false };
        var layer = new OpenLayers.Layer('Test Layer', options);
        mapone.addLayer(layer);
        layer.setVisibility(true);

        // randomly assigned property
        layer.chocolate = 5;

        var clone = layer.clone();

        t.ok( clone.map == null, "cloned layer has map property set to null")

        var maptwo = new OpenLayers.Map('map2'); 
        maptwo.addLayer(clone);

        t.ok( clone instanceof OpenLayers.Layer, "new OpenLayers.Layer returns object" );
        t.eq( clone.name, "Test Layer", "default clone.name is correct" );
        t.ok( ((clone.options["chicken"] == 151) && (clone.options["foo"] == "bar")), "clone.options correctly set" );
        t.eq(clone.chocolate, 5, "correctly copied randomly assigned property");

        t.eq(clone.visibility, true, "visibility correctly cloned");
        
        layer.addOptions({chicken:152});
        t.eq(clone.options["chicken"], 151, "made a clean copy of options");        

        mapone.destroy();
        maptwo.destroy();
    }

    function test_Layer_setName (t) {
        
        t.plan( 1 );

        layer = new OpenLayers.Layer('Test Layer');
        layer.setName("chicken");
        
        t.eq(layer.name, "chicken", "setName() works")
        
    }

    function test_Layer_addOptions (t) {
        
        t.plan( 20 );

        var map = new OpenLayers.Map("map", {allOverlays: true});
        var options = { chicken: 151, foo: "bar" };
        var layer = new OpenLayers.Layer('Test Layer', options);
        map.addLayer(layer);

        layer.addOptions({bark:55, chicken: 171});
        t.eq(layer.bark, 55, "addOptions() assigns new option correctly to Layer");
        t.eq(layer.options.bark, 55, "addOptions() adds new option correctly to backup");

        t.eq(layer.chicken, 171, "addOptions() overwrites option correctly to Layer");
        t.eq(layer.options.chicken, 171, "addOptions() overwrites option correctly to backup");

        var log;
        layer.initResolutions = function() {
            log++;
        };
        log = 0;
        layer.addOptions({bark: 56});
        t.eq(log, 0, "addOptions doesn't call initResolutions when not given a resolution option");

        log = 0;
        layer.addOptions({scales: [1, 2]});
        t.eq(log, 1, "addOptions calls initResolutions when given scales");

        log = 0;
        layer.addOptions({resolutions: [1, 2]});
        t.eq(log, 1, "addOptions calls initResolutions when given resolutions");

        log = 0;
        layer.addOptions({minScale: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given minScale");

        log = 0;
        layer.addOptions({maxScale: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxScale");

        log = 0;
        layer.addOptions({minResolution: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given minResolution");

        log = 0;
        layer.addOptions({maxResolution: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxResolution");

        log = 0;
        layer.addOptions({numZoomLevels: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given numZoomLevels");

        log = 0;
        layer.addOptions({maxZoomLevel: 4});
        t.eq(log, 1, "addOptions calls initResolutions when given maxZoomLevel");

        log = 0;
        layer.addOptions({projection: new OpenLayers.Projection("EPSG:900913")});
        t.eq(log, 1, "addOptions calls initResolutions when given projection");

        log = 0;
        layer.addOptions({units: "m"});
        t.eq(log, 1, "addOptions calls initResolutions when given units");

        log = 0;
        layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 1, "addOptions calls initResolutions when given minExtent");

        log = 0;
        layer.addOptions({maxExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 1, "addOptions calls initResolutions when given maxExtent");

        layer.projection = null;
        layer.addOptions({projection: "EPSG:900913"});
        t.ok(layer.projection instanceof OpenLayers.Projection,
             "addOptions creates a Projection object when given a projection string");
        
        log = null;
        // adding a 2nd layer to see if it gets reinitialized properly
        var layer2 = new OpenLayers.Layer(null, {
            moveTo: function(bounds) {
                log = bounds;
            }
        });
        map.addLayer(layer2);
        layer.addOptions({maxResolution: 0.00034332275390625}, true);
        t.eq(log.toBBOX(), map.getExtent().toBBOX(), "when reinitialize is set to true, changing base layer's resolution property reinitializes all layers.");

        map.removeLayer(layer);
        log = 0;
        layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)});
        t.eq(log, 0, "addOptions doesn't call initResolutions when layer is not in map");
    }
    
    function test_addOptionsScale(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.WMS();
        map.addLayer(layer);
        map.zoomToMaxExtent();
        layer.addOptions({maxResolution: 0.5, numZoomLevels: 15});
        t.eq(layer.alwaysInRange, false, "alwaysInRange should not be true anymore");
    }
    
    function test_Layer_StandardOptionsAccessors (t) {

        t.plan( 4 );

        var projection = "EPSG:4326";
        var maxExtent = new OpenLayers.Bounds(50,50,100,100);
        var maxResolution = 1.5726;
        var numZoomLevels = 11;

        var options = { projection: projection, 
                        maxExtent: maxExtent,
                        maxResolution: maxResolution,
                        numZoomLevels: numZoomLevels
                        };

        var layer = new OpenLayers.Layer('Test Layer', options);

        t.eq(layer.projection.getCode(), projection, "projection set correctly");
        t.ok(layer.maxExtent.equals(maxExtent), "maxExtent set correctly");
        t.eq(layer.maxResolution, maxResolution, "maxResolution set correctly");
        t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly");
    }
    
    function test_maxExtent(t) {
        t.plan(5);
        
        var layer = new OpenLayers.Layer(
            null, {maxExtent: [-180, 0, 0, 90]}
        );
        
        t.ok(layer.maxExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
        t.eq(layer.maxExtent.left, -180, "(array) bounds left");
        t.eq(layer.maxExtent.bottom, 0, "(array) bounds left");
        t.eq(layer.maxExtent.right, 0, "(array) bounds right");
        t.eq(layer.maxExtent.top, 90, "(array) bounds top");
        
        layer.destroy();
    }

    function test_minExtent(t) {
        t.plan(5);
        
        var layer = new OpenLayers.Layer(
            null, {minExtent: [-180, 0, 0, 90]}
        );
        
        t.ok(layer.minExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
        t.eq(layer.minExtent.left, -180, "(array) bounds left");
        t.eq(layer.minExtent.bottom, 0, "(array) bounds left");
        t.eq(layer.minExtent.right, 0, "(array) bounds right");
        t.eq(layer.minExtent.top, 90, "(array) bounds top");
        
        layer.destroy();
    }


    function test_eventListeners(t) {
        t.plan(1);
        
        var method = OpenLayers.Events.prototype.on;
        // test that events.on is called at layer construction
        var options = {
            eventListeners: {foo: "bar"}
        };
        OpenLayers.Events.prototype.on = function(obj) {
            t.eq(obj, options.eventListeners, "events.on called with eventListeners");
        }
        var layer = new OpenLayers.Layer("test", options);
        OpenLayers.Events.prototype.on = method;
        layer.destroy();
        
        // if events.on is called again, this will fail due to an extra test
        // test layer without eventListeners
        OpenLayers.Events.prototype.on = function(obj) {
            t.fail("events.on called without eventListeners");
        }
        var layer2 = new OpenLayers.Layer("test");
        OpenLayers.Events.prototype.on = method;
        layer2.destroy();
    }

    function test_initResolutions_alwaysInRange(t) {
        t.plan(3);

        var map, layer;

        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("test", {maxResolution: 80, minResolution: 10});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, false,
             "alwaysInRange set to false due to passed options");
        map.destroy();

        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("test", {projection: "unknown"});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, true,
             "alwaysInRange true if unknown projection is set.");
        map.destroy();

        map = new OpenLayers.Map("map");
        OpenLayers.Layer.prototype.alwaysInRange = false;
        layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326'});
        map.addLayer(layer);
        t.eq(layer.alwaysInRange, false,
             "alwaysInRange true if overridden on prototype.");
        OpenLayers.Layer.prototype.alwaysInRange = null;
        map.destroy();
    }

    function test_initResolutions_resolutions(t) {

        function initResolutionsTest(
            id, mapOptions, layerOptions,
            expectedResolutions, expectedMinResolution, expectedMaxResolution) {

            // setup
            var map = new OpenLayers.Map("map", mapOptions);
            var layer = new OpenLayers.Layer(null, layerOptions);
            map.addLayer(layer);

            // make resolution assertions
            t.eq(
                layer.resolutions.length, expectedResolutions.length,
                id + ": correct resolutions length"
            );
            // allow for floating point imprecision
            var got, exp, same = true;
            for (var i=0; i<expectedResolutions.length; ++i) {
                got = layer.resolutions[i];
                exp = expectedResolutions[i];
                if (got.toFixed(10) !== exp.toFixed(10)) {
                    t.fail(id + ": bad resolutions - index " + i + " got " + got + " but expected " + exp);
                    same = false;
                    break;
                }
            }
            if (same) {
                t.ok(true, id + ": correct resolutions");
            }
            t.eq(
                layer.maxResolution, expectedMaxResolution,
                id + ": maxResolution set"
            );
            t.eq(
                layer.minResolution, expectedMinResolution,
                id + ": minResolution set"
            );

            // teardown
            map.destroy();
        }

        // each case is an array of id, map options, layer options, expected resolutions,
        // expected min resolution, and expected max resolution
        var cases = [[

        /*
         * Batch 1: map defaults and sensible layer options
         */

            "1.0",  null, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "1.1",  null, {resolutions: [400, 200, 100], minResolution: 150, maxResolution: 300},
            [400, 200, 100], 150, 300
        ], [
            "1.2",  null, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "1.3",  null, {maxResolution: 4000, maxZoomLevel: 2},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "1.4",  null, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "1.5",  null, {minResolution: 40, maxZoomLevel: 2},
            [160, 80, 40], 40, 160
        ], [
            "1.6", null, {minResolution: 10, maxResolution: 40},
            [40, 20, 10], 10, 40
        ], [
            "1.7", null, {minResolution: 10, maxResolution: 40, numZoomLevels: 3},
            [40, 20, 10], 10, 40
        ], [
            "1.8", null, {minResolution: 10, maxResolution: 40, maxZoomLevel: 2},
            [40, 20, 10], 10, 40
        ], [
            "1.9",  null, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.10",  null, {scales: [400000, 200000, 100000], minScale: 400000, maxScale: 100000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.11",  null, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.12",  null, {minScale: 400000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.13",  null, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.14",  null, {maxScale: 100000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.15",  null, {maxScale: 100000, minScale: 400000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.16",  null, {maxScale: 100000, minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.17",  null, {maxScale: 100000, minScale: 400000, maxZoomLevel: 2},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "1.18",  null, {scales: [400000, 200000, 100000], units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.19",  null, {minScale: 400000, numZoomLevels: 3, units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.20",  null, {maxScale: 100000, numZoomLevels: 3, units: "m"},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
            "1.21", null, {numZoomLevels: 2},  // maxResolution calculated based on the projection's maxExtent here
           [1.40625, 0.703125], 0.703125, 1.40625
        ], [

        /*
         * Batch 2: custom map options map and sensible layer options
         */

        /*
         * Batch 2.1: resolutions set in the layer options
         */
            "2.1.0",  {resolutions: [300, 150]}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.1",  {numZoomLevels: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.2",  {maxResolution: 300}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.3",  {minResolution: 300}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.4",  {scales: [4, 2, 1]}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.5",  {minScale: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
            "2.1.6",  {maxScale: 4}, {resolutions: [400, 200, 100]},
            [400, 200, 100], 100, 400
        ], [
        /*
         * Batch 2.2: minResolution and maxResolution set in the layer options
         */
            "2.2.0",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: 0},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.1",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: -1},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.2",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: null},
            [80, 40, 20, 10], 12, 48
        ], [
            "2.2.3",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: undefined},
            [48, 24, 12], 12, 48
        ], [
            "2.2.4",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48, numZoomLevels: 3},
            [48, 24, 12], 12, 48
        ], [
            "2.2.5",  {resolutions: [80, 40, 20, 10]}, {minResolution: 12, maxResolution: 48},
            [48, 24, 12], 12, 48
        ], [
        /*
         * Batch 2.3: maxResolution set in the layer options
         */
            "2.3.0",  {resolutions: [300, 150]}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.1",  {numZoomLevels: 2}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.2",  {maxResolution: 50}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.3",  {scales: [4, 2, 1]}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.4",  {minScale: 4}, {maxResolution: 4000, numZoomLevels: 3},
            [4000, 2000, 1000], 1000, 4000
        ], [
            "2.3.5",  {resolutions: [300, 150]}, {maxResolution: 250},
            [300, 150], 150, 250
        ], [
        /*
         * Batch 2.4: minResolution set in the layer options
         */
            "2.4.0",  {resolutions: [300, 150]}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.1",  {numZoomLevels: 2}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.2",  {minResolution: 50}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.3",  {scales: [4, 2, 1]}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.4",  {maxScale: 1}, {minResolution: 40, numZoomLevels: 3},
            [160, 80, 40], 40, 160
        ], [
            "2.4.5",  {resolutions: [300, 150]}, {minResolution: 250},
            [300, 150], 250, 300
        ], [
        /*
         * Batch 2.5: scales set in the layer options
         */
            "2.5.0",  {resolutions: [4, 2, 1]}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.1",  {numZoomLevels: 2}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.2",  {maxResolution: 4}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.3",  {minResolution: 1}, {scales: [400000, 200000, 100000]},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.5.4",  {units: "m"}, {scales: [400000, 200000, 100000]},
            [141.11139333389778, 70.55569666694889, 35.277848333474445], 35.277848333474445, 141.11139333389778
        ], [
        /*
         * Batch 2.6: minScale set in the layer options
         */
            "2.6.0",  {resolutions: [4, 2, 1]}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.1",  {numZoomLevels: 2}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.2",  {maxResolution: 4}, {minScale: 400000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.6.3", {scales: [400000, 200000, 100000]}, {minScale: 200000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0006349563376084181
        ], [
        /*
         * Batch 2.7: maxScale set in the layer options
         */
            "2.7.0",  {resolutions: [4, 2, 1]}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.1",  {numZoomLevels: 2}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.2",  {minResolution: 1}, {maxScale: 100000, numZoomLevels: 3},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.00031747816880420905, 0.0012699126752168362
        ], [
            "2.7.3", {scales: [400000, 200000, 100000]}, {maxScale: 200000},
            [0.0012699126752168362, 0.0006349563376084181, 0.00031747816880420905], 0.0006349563376084181, 0.0012699126752168362
        ], [
        /*
         * Batch 2.8: numZoomLevels set in the layer options
         */
            "2.8.0",  {maxResolution: 80}, {numZoomLevels: 4},  // maxResolution calculated based on the projection's maxExtent here
            [1.40625, 0.703125, 0.3515625, 0.17578125], 0.17578125, 1.40625
        ], [
            "2.8.1", {maxResolution: 80, numZoomLevels: 4}, {numZoomLevels: null},
            [80, 40, 20, 10], 10, 80
        ], [
            "2.8.2", {maxResolution: 80, numZoomLevels: 4}, {numZoomLevels: undefined},
            [80, 40, 20, 10], 10, 80
        ]];

        // run all cases (4 tests each)
        var i, num = cases.length, c;
        t.plan(num * 4);
        for (i=0; i<num; ++i) {
            c = cases[i];
            initResolutionsTest(c[0], c[1], c[2], c[3], c[4], c[5]);
        }

    }

    function test_Layer_visibility(t) {

        t.plan(7);

        var layer = new OpenLayers.Layer('Test Layer');
    
        t.eq(layer.getVisibility(), true, "default for layer creation is visible");
        
        layer.setVisibility(false);
        t.eq(layer.getVisibility(), false, "setVisibility false works");

        layer.setVisibility(true);
        t.eq(layer.getVisibility(), true, "setVisibility true works");
       
        // Need a map in order to have moveTo called.
        // Tests added for #654.
        var layer = new OpenLayers.Layer.WMS('Test Layer','http://example.com');
        var m = new OpenLayers.Map('map');
        m.addLayer(layer);
        m.zoomToMaxExtent();
        
        layermoved = false; 
        layer.moveTo = function() { layermoved = true; }
        
        layer.events.register('visibilitychanged', t, function() {
            this.ok(true, "Visibility changed calls layer event.");
        });    
        
        layer.setVisibility(false);
        t.eq(layermoved, false, "Layer didn't move when calling setvis false");
        
        layer.setVisibility(true);
        t.eq(layermoved, true, "Layer moved when calling setvis true.");
    
    }


    function test_Layer_getZoomForResolution(t) {

        t.plan(12);

        var layer = new OpenLayers.Layer('Test Layer');
        layer.map = {};
        
        //make some dummy resolutions
        layer.resolutions = [128, 64, 32, 16, 8, 4, 2];
        
        t.eq(layer.getZoomForResolution(200), 0, "zoom all the way out");
        t.eq(layer.getZoomForResolution(25), 2, "zoom in middle");
        t.eq(layer.getZoomForResolution(3), 5, "zoom allmost all the way in");
        t.eq(layer.getZoomForResolution(1), 6, "zoom  all the way in");

        t.eq(layer.getZoomForResolution(65), 0, "smallest containing res");
        t.eq(layer.getZoomForResolution(63), 1, "smallest containing res");

        t.eq(layer.getZoomForResolution(65, true), 1, "closest res");
        t.eq(layer.getZoomForResolution(63, true), 1, "closest res");
        
        layer.map.fractionalZoom = true;
        t.eq(layer.getZoomForResolution(64), 1,
             "(fractionalZoom) correct zoom for res in array");
        t.eq(layer.getZoomForResolution(48).toPrecision(6), (1.5).toPrecision(6),
             "(fractionalZoom) linear scaling for res between entries");
        t.eq(layer.getZoomForResolution(200).toPrecision(6), (0).toPrecision(6),
             "(fractionalZoom) doesn't return zoom below zero");
        t.eq(layer.getZoomForResolution(1).toPrecision(6), (layer.resolutions.length - 1).toPrecision(6),
             "(fractionalZoom) doesn't return zoom above highest index");
    }
    
    function test_Layer_redraw(t) {
        t.plan(11)

        var name = 'Test Layer';
        var url = "http://octo.metacarta.com/cgi-bin/mapserv";
        var params = { map: '/mapdata/vmap_wms.map', 
                       layers: 'basic', 
                       format: 'image/jpeg'};

        var layer = new OpenLayers.Layer.WMS(name, url, params);
        
        t.ok(!layer.redraw(),
             "redraw on an orphan layer returns false");
        
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);

        t.ok(!layer.redraw(),
             "redraw returns false if map does not yet have a center");
        map.zoomToMaxExtent();
        
        t.ok(layer.redraw(),
             "redraw returns true after map has a center");
        
        layer.setVisibility(false);
        t.ok(!layer.redraw(),
             "redraw returns false if a layer is not visible");
        
        layer.setVisibility(true);
        t.ok(layer.redraw(),
                "redraw returns true even if extent has not changed");
        
        var log = {};
        var onMoveend = function(e) {
            log.event = e;
        };
        layer.events.on({"moveend": onMoveend});
        layer.redraw();
        layer.events.un({"moveend": onMoveend});
        // test that the moveend event was triggered
        t.ok(log.event, "an event was logged");
        t.eq(log.event.type, "moveend", "moveend was triggered");
        t.eq(log.event.zoomChanged, true, "event says zoomChanged true - poor name");

        layer.moveTo = function(bounds, zoomChanged, dragging) {
            var extent = layer.map.getExtent();
            t.ok(bounds.equals(extent),
                 "redraw calls moveTo with the map extent");
            t.ok(zoomChanged,
                 "redraw calls moveTo with zoomChanged true");
            t.ok(!dragging,
                 "redraw calls moveTo with dragging false");
        }
        layer.redraw();
    }
      
    function test_layer_setIsBaseLayer(t) {
        t.plan(2);
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer();

        map.events.register("changebaselayer", t, function() {
          this.ok(true, "setIsBaseLayer() trig changebaselayer event.")
        });

        map.addLayer(layer);
        layer.setIsBaseLayer(true);
        t.ok(layer.isBaseLayer, "setIsBaseLayer() change isBaseLayer property.");
    }
      
    function test_layer_setTileSize(t) {
        t.plan(4);

        layer = new OpenLayers.Layer();
        
        g_MapTileSize = new OpenLayers.Size(25,67);        
        layer.map = {
            getTileSize: function() {
                return g_MapTileSize;
            }
        };

        var layerTileSize = new OpenLayers.Size(1,1);

    //TILE SIZE
        layer.tileSize = layerTileSize;
      
      //parameter
        var size = new OpenLayers.Size(2,2);
        layer.setTileSize(size);
        t.ok(layer.tileSize.equals(size), "size paramater set correctly to layer's tile size");
      
      //set on layer
        layer.tileSize = layerTileSize;
        layer.setTileSize();
        t.ok(layer.tileSize.equals(layerTileSize), "layer's tileSize property preserved if no parameter sent in");
      
      //take it from map
        layer.tileSize = null;
        layer.setTileSize();
        t.ok(layer.tileSize.equals(g_MapTileSize), "layer's tileSize property is null and so correctly taken from the map");
        
        

    //GUTTERS
        layer.gutter = 15;
        size = new OpenLayers.Size(10,100);
        layer.setTileSize(size);

        var desiredImageSize = new OpenLayers.Size(40, 130); 

        t.ok(layer.imageSize.equals(desiredImageSize), "image size correctly calculated");
    }
    
    function test_Layer_getResolution(t) {
        t.plan(1);
        var layer = new OpenLayers.Layer("test");
        layer.map = {
            getZoom: function() {return "foo";}
        };
        layer.getResolutionForZoom = function(zoom) {
            t.eq(zoom, "foo", "getResolution calls getResolutionForZoom");
        }
        layer.getResolution();
        layer.map = null;
        layer.destroy();
    }
    
    function test_Layer_getResolutionForZoom(t) {
        t.plan(8);
        var layer = new OpenLayers.Layer("test");
        layer.map = {fractionalZoom: false};
        layer.resolutions = ["zero", "one", "two"];
        t.eq(layer.getResolutionForZoom(0), "zero",
             "(fractionalZoom false) returns resolution for given index");
        t.eq(layer.getResolutionForZoom(0.9), "one",
             "(fractionalZoom false) returns resolution for float index");
        
        layer.resolutions = [2, 4, 6, 8];
        layer.map.fractionalZoom = true;
        t.eq(layer.getResolutionForZoom(1).toPrecision(6), (4).toPrecision(6),
             "(fractionalZoom true) returns resolution for integer zoom");

        t.eq(layer.getResolutionForZoom(1.3).toPrecision(6), (4.6).toPrecision(6),
             "(fractionalZoom true) for zoom 1.3 should be 4.6");

        t.eq(layer.getResolutionForZoom(1.6).toPrecision(6), (5.2).toPrecision(6),
             "(fractionalZoom true) for zoom 1.6 should be 5.2");

        t.eq(layer.getResolutionForZoom(1.8).toPrecision(6), (5.6).toPrecision(6),
             "(fractionalZoom true) for zoom 1.8 should be 5.6");

        t.eq(layer.getResolutionForZoom(1.5).toPrecision(6), (5).toPrecision(6),
             "(fractionalZoom true) returns resolution for float zoom");             
        t.eq(layer.getResolutionForZoom(3.5).toPrecision(6), (8).toPrecision(6),
             "(fractionalZoom true) returns resolution for zoom beyond res length - 1");
        
    }
    
    function test_afterAdd(t) {
        
        t.plan(4);
        
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer(null, {
            isBaseLayer: true,
            eventListeners: {
                "added": function(evt) {
                    log.push(evt);
                }
            }
        });
        var hasBase = false;
        layer.afterAdd = function() {
            hasBase = !!(layer.map && layer.map.baseLayer);
        }
        map.addLayer(layer);
        t.eq(hasBase, true, "when afterAdd is called, map has a base layer");
        t.eq(log.length, 1, "added event triggered");
        t.eq(log[0].map.id, map.id, "added listener argument with correct map");
        t.eq(log[0].layer.id, layer.id, "added listener argument with correct layer");
        
    }
    
    function test_setOpacity(t) {
        t.plan(5);

        var map, layer, log;
        
        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer("");
        map.addLayer(layer);

        log = [];
        map.events.register('changelayer', t, function(event) {
            log.push({layer: event.layer, property: event.property});
        });
        layer.setOpacity(0.42);
        t.eq(layer.opacity, 0.42,
             "setOpacity() set layer.opacity to correct value");
        t.eq(log.length, 1,
             "setOpacity() triggers changelayer once");
        t.ok(log[0].layer == layer,
             "changelayer listener called with expected layer");
        t.eq(log[0].property, "opacity",
             "changelayer listener called with expected property");

        // This call must not trig the event because the opacity value is the same.
        log = [];
        layer.setOpacity(0.42);
        t.eq(log.length, 0,
             "setOpacity() does not trigger changelayer if the opacity value is the same");
    }


/******
 * 
 * 
 * HERE IS WHERE SOME TESTS SHOULD BE PUT TO CHECK ON THE LONLAT-PX TRANSLATION
 * FUNCTIONS AND RESOLUTION AND GETEXTENT GETZOOMLEVEL, ETC
 * 
 * 
 */


    function test_Layer_destroy (t) {
        t.plan( 8 );    

        var log = [];
        var map = new OpenLayers.Map('map');
        
        layer = new OpenLayers.Layer('Test Layer', {
            eventListeners: {
                "removed": function(evt) {
                    log.push(evt);
                }
            }
        });

        map.addLayer(layer);

        layer.destroy();

        t.eq( layer.name, null, "layer.name is null after destroy" );
        t.eq( layer.div, null, "layer.div is null after destroy" );
        t.eq( layer.map, null, "layer.map is null after destroy" );
        t.eq( layer.options, null, "layer.options is null after destroy" );

        t.eq(map.layers.length, 0, "layer removed from map");
        t.eq(log.length, 1, "removed event triggered");
        t.eq(log[0].map.id, map.id, "removed listener argument with correct map");
        t.eq(log[0].layer.id, layer.id, "removed listener argument with correct layer");
        
        map.destroy();

    }

  </script>
</head>
<body>
  <div id="map" style="width:500px;height:500px"></div>
  <div id="map2" style="width:100px;height:100px"></div>
</body>
</html>
